package com.test32_网络编程.echo.client;

import com.test32_网络编程.util.KeyboardInputUtil;

import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class EchoClient {

    public static void main(String[] args) throws Exception {
        Socket client = new Socket("localhost", 9999);
        Scanner scanner = new Scanner(client.getInputStream());         // 客户端输入流
        PrintStream out = new PrintStream(client.getOutputStream());    // 客户端输出流
        boolean flag = true;
        while (flag) {
            String value = KeyboardInputUtil.getString("请输入要发送的数据：");
            out.println(value);
            if (scanner.hasNext()) {
                System.out.println("【Echo客户端】" + scanner.next());    // 服务器端响应
            }
            if (value.equalsIgnoreCase("exit")) {   // 结束交互
                flag = false;
            }
        }
        client.close();
    }
}
